/*
 * 版权所有(C)，上海海鼎信息工程股份有限公司，lizhixing，所有权利保留。
 * <p>
 * 项目名：	boot-demo
 * 文件名：	TokenFilter
 * 模块说明：
 * 修改历史：
 * 2022/2/12 - lizhixing - 创建。
 */
package com.lovely602.boot.bootdemo.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
 * @author lizhixing
 */
@WebFilter(filterName = TokenFilter.FILTER_NAME, urlPatterns = "/*")
public class TokenFilter implements Filter {

  public static final String FILTER_NAME = "tokenFilter";

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    System.out.println("tokenFilter init...");
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    System.out.println("tokenFilter doFilter...");
    TokenContext tokenContext = new TokenContext();
    tokenContext.setTenantId("tenantId");
    tokenContext.setOrgType("orgType");
    tokenContext.setOrgId("orgId");
    tokenContext.setUserId("userId");
    tokenContext.setUserName("userName");
    tokenContext.setEmployeeId("employeeId");
    tokenContext.setEmployeeCode("employeeCode");
    tokenContext.setEmployeeName("employeeName");
    TokenContextHolder.setTokenContext(tokenContext);
    chain.doFilter(request, response);
  }

  @Override
  public void destroy() {
    System.out.println("tokenFilter destroy...");
  }
}
